home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ für Kids
/
C++ for kids.iso
/
Buch
/
Bubble1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-01-25
|
2KB
|
83 lines
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "Bubble1.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
template <class AllTyp>
void ExChange (AllTyp &x, AllTyp &y)
{
AllTyp z = x; x = y; y = z;
}
template <class AllTyp>
AllTyp BubbleSort (AllTyp *Element, int Max)
{
for (int x=0; x<Max; x++)
for (int y=x; y<Max; y++)
if (Element[x] > Element[y])
ExChange (Element[x], Element[y]);
return Element[0];
}
const int Max = 30;
String Kette[Max];
int Zahl[Max];
bool Modus;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
randomize ();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (OpenDialog1->Execute())
{
Memo1->Lines->LoadFromFile (OpenDialog1->FileName);
for (int i=0; i<Max; i++)
{
Kette[i] = Memo1->Lines->Strings[i];
}
}
Modus = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
for (int i=0; i<Max; i++)
{
Zahl[i] = random(100000)+1000;
Memo1->Lines->Add (IntToStr (Zahl[i]));
}
Modus = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Memo1->Clear ();
if (Modus)
{
BubbleSort (Kette, Max);
for (int i=0; i<Max; i++)
Memo1->Lines->Add (Kette[i]);
}
else
{
BubbleSort (Zahl, Max);
for (int i=0; i<Max; i++)
Memo1->Lines->Add (IntToStr (Zahl[i]));
}
}
//---------------------------------------------------------------------------